/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BUS.LoaiPhong_BUS;
import BUS.Phong_BUS;
import DTO.LoaiPhong_DTO;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lenh Ho Xung
 */
public final class LoaiPhong extends javax.swing.JFrame {

    DefaultTableModel modelLoaiPhong;

    /**
     * Creates new form LoaiPhong
     */
    public LoaiPhong() {
        initComponents();
        showLoaiPhong();
        btCapNhatLoaiPhong.setEnabled(false);
        btXoaLoaiPhong.setEnabled(false);
    }

    public void showLoaiPhong() {
        try {
            modelLoaiPhong = LoaiPhong_BUS.layDanhSachLoaiPhong();
            tbLoaiPhong.setModel(modelLoaiPhong);
        } catch (Exception ex) {
            Logger.getLogger(LoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tfTenLoaiPhong = new javax.swing.JTextField();
        tfDonGia = new javax.swing.JTextField();
        btThemLoaiPhong = new javax.swing.JButton();
        btXoaLoaiPhong = new javax.swing.JButton();
        btCapNhatLoaiPhong = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbLoaiPhong = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Tên loại phòng: ");

        jLabel2.setText("Đơn giá:");

        btThemLoaiPhong.setText("Thêm");
        btThemLoaiPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btThemLoaiPhongActionPerformed(evt);
            }
        });

        btXoaLoaiPhong.setText("Xóa");
        btXoaLoaiPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btXoaLoaiPhongActionPerformed(evt);
            }
        });

        btCapNhatLoaiPhong.setText("Cập nhật");
        btCapNhatLoaiPhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCapNhatLoaiPhongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(37, 37, 37)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btThemLoaiPhong)
                        .addGap(30, 30, 30)
                        .addComponent(btXoaLoaiPhong)
                        .addGap(18, 18, 18)
                        .addComponent(btCapNhatLoaiPhong))
                    .addComponent(tfTenLoaiPhong, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btCapNhatLoaiPhong, btThemLoaiPhong, btXoaLoaiPhong});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(tfTenLoaiPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(tfDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCapNhatLoaiPhong)
                    .addComponent(btXoaLoaiPhong)
                    .addComponent(btThemLoaiPhong))
                .addGap(20, 20, 20))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btCapNhatLoaiPhong, btThemLoaiPhong, btXoaLoaiPhong});

        tbLoaiPhong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbLoaiPhong.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbLoaiPhongMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbLoaiPhong);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 513, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbLoaiPhongMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbLoaiPhongMouseClicked
        // TODO add your handling code here:
        int row = tbLoaiPhong.getSelectedRow();
        if (row >= 0) {
            btCapNhatLoaiPhong.setEnabled(true);
            btXoaLoaiPhong.setEnabled(true);
        }
        String tenloaiPhong = modelLoaiPhong.getValueAt(row, 1).toString();
        String dongia = modelLoaiPhong.getValueAt(row, 2).toString();
        tfTenLoaiPhong.setText(tenloaiPhong);
        tfDonGia.setText(dongia);
    }//GEN-LAST:event_tbLoaiPhongMouseClicked

    private void btThemLoaiPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btThemLoaiPhongActionPerformed
        // TODO add your handling code here:
        int rows = tbLoaiPhong.getRowCount();
        float dongia;
        LoaiPhong_DTO p = new LoaiPhong_DTO();
        try {

            if (rows <= 0) {
                p.setMaloaiPhong(Phong_BUS.taoMaTuDong("MLP0000", 3));
            } else {
                p.setMaloaiPhong(Phong_BUS.taoMaTuDong(modelLoaiPhong.getValueAt(rows - 1, 0).toString(), 3));
            }
            if (tfTenLoaiPhong.getText().equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập tên loại phòng!");
                return;
            } else {
                p.setTenloaiPhong(tfTenLoaiPhong.getText());
            }

            try {
                dongia = Float.parseFloat(tfDonGia.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số!");
                return;
            }
           
            if (tfDonGia.getText().equals("")) {
                JOptionPane.showMessageDialog(rootPane, "Bạn phải nhập đơn giá!");
                return;
            } else {
                 p.setDonGia(Float.toString(dongia));
            }
            LoaiPhong_BUS.Insert(p);

        } catch (Exception ex) {
            Logger.getLogger(LoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showLoaiPhong();
    }//GEN-LAST:event_btThemLoaiPhongActionPerformed

    private void btXoaLoaiPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btXoaLoaiPhongActionPerformed
        // TODO add your handling code here:
        int row = this.tbLoaiPhong.getSelectedRow();
       int show = JOptionPane.showConfirmDialog(rootPane, "Bạn có chắc chắn muốn xóa?", "", JOptionPane.YES_NO_OPTION);
        if (show == JOptionPane.YES_OPTION) {     
            try {
            String maloaiPhong = (String) this.modelLoaiPhong.getValueAt(row, 0).toString();
            LoaiPhong_BUS.Delete(maloaiPhong);

        } catch (Exception e) {
        }
        showLoaiPhong();
        }
        else{
            return;
        }
    }//GEN-LAST:event_btXoaLoaiPhongActionPerformed

    private void btCapNhatLoaiPhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCapNhatLoaiPhongActionPerformed
        // TODO add your handling code here:
        int row = tbLoaiPhong.getSelectedRow();
        float dongia;
        LoaiPhong_DTO p = new LoaiPhong_DTO();
        try {
            p.setMaloaiPhong(modelLoaiPhong.getValueAt(row, 0).toString());
            p.setTenloaiPhong(tfTenLoaiPhong.getText());
            try {
                dongia = Float.parseFloat(tfDonGia.getText());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "Đơn giá phải là số!");
                return;
            }
            p.setDonGia(Float.toString(dongia));
            LoaiPhong_BUS.Update(p);

        } catch (Exception ex) {
            Logger.getLogger(LoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
        showLoaiPhong();
    }//GEN-LAST:event_btCapNhatLoaiPhongActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoaiPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoaiPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoaiPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoaiPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoaiPhong().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCapNhatLoaiPhong;
    private javax.swing.JButton btThemLoaiPhong;
    private javax.swing.JButton btXoaLoaiPhong;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbLoaiPhong;
    private javax.swing.JTextField tfDonGia;
    private javax.swing.JTextField tfTenLoaiPhong;
    // End of variables declaration//GEN-END:variables
}
